package com.lovo.frame;

import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPanel extends JPanel implements ActionListener {
//    private Icon bgLogin = Util.getIcon("/images/buttonLand.jpg");
    private JButton loginButton = new JButton("登录");
    private ATM frame;
    private JTextField textField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();
    private int acount = 0;
    private int index;
    private UserService userService = new UserServiceImpl();

    public void setAcount(int acount) {
        this.acount = acount;
    }

    public LoginPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        loginButton.setBounds(450, 450, 100, 50);
        this.add(loginButton);
        loginButton.addActionListener(this);
        JLabel label = new JLabel("账号");
        label.setBounds(350, 300, 50, 20);
        this.add(label);
        textField.setBounds(400, 300, 100, 20);
        this.add(textField);
        JLabel label1 = new JLabel("密码");
        label1.setBounds(350, 350, 50, 20);
        this.add(label1);
        passwordField.setBounds(400, 350, 100, 20);
        this.add(passwordField);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginButton) {
//            boolean flag = true;
//            for (int i = 0; i < com.lovo.frame.getList().size(); i++) {
//                if (com.lovo.frame.getList().get(i).getName().equals(textField.getText()) && com.lovo.frame.getList().get(i).getPwd().equals(passwordField.getText())) {
//                    JOptionPane.showMessageDialog(this, "登陆成功");
//                    com.lovo.frame.switchPanel("menu");
//                    index=i;
//                    return;
//                }
//            }
            String account = textField.getText();
            String pwd = String.valueOf(passwordField.getPassword());
            UserDO user = userService.getByusernameAndPwd(account, pwd);
            if (user != null) {
                frame.setLoginUser(user);
                frame.switchPanel("menu");
                return;
            }
            acount++;
            if (acount == 2) {
                JOptionPane.showMessageDialog(this, "次数用光账号冻结");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "账号密码错误,还剩" + (3 - acount) + "次机会");
        }
    }
}

